/*
 * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
 * Copyright (c) 2010-2011 Oracle Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **/

package org.sunspotworld.demo;

import com.sun.spot.core.util.Utils;
import com.sun.spot.ipv6.io.j2me.tcp.TCPConnection;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.io.Connector;

/**
 * Sample Sun SPOT host application
 */
public class TCPClient {

    private static final int PORT_NUMBER = 8888;

    /**
     * contact remote target and read values
     */
    public void run(String target) {
        TCPConnection sendConn;
        DataOutputStream tout = null;
        DataInputStream tin = null;


        String url;

        Utils.sleep(1000);  // Let IPv6 startup under us
        System.out.println("[APP] Sending data to = " + target);
        boolean isIP6Addr = (target.indexOf(":") > -1);
        if (isIP6Addr) {
            url = "tcp://[" + target + "]:" + PORT_NUMBER;
        } else {
            // we hope it is an IEEEAddress or hostname
            url = "tcp://" + target + ":" + PORT_NUMBER;
        }

        try {
            System.out.println("[APP] URL: " + url);
            sendConn = (TCPConnection) Connector.open(url);
            sendConn.setTimeout(3000);
            tout = sendConn.openDataOutputStream();
            tin = sendConn.openDataInputStream();

            int count = tin.readInt();
            String node = tin.readUTF();
            int light = tin.readInt();
            double temp = tin.readDouble();
            System.out.println("[APP] Previous Connections: " + count + "\tLast: " + node);
            System.out.println("[APP] Light: " + light + "\tTemperature: " + temp);

            tin.close();
            tout.close();
            sendConn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.exit(0);
    }

    /**
     * Start up the host application.
     *
     * @param args any command line arguments
     */
    public static void main(String[] args) {

        if (args.length < 1) {
            System.out.println("TCPClient: must provide target address");
            System.out.println("Usage: ant host-run -Dmain.args=<target address>");
            System.exit(0);
        }
        
        TCPClient app = new TCPClient();
        app.run(args[0]);
    }
}
